/*
 * Copyright 2021 <copyright holder> <email>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MAP_H
#define MAP_H

#include "common_include.h"
#include "frame.h"
#include "mappoint.h"

/**
 * @todo write docs
 */
class Map
{
public:
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
    typedef std::shared_ptr<Map> Ptr;
    typedef std::unordered_map<unsigned long, Mappoint::Ptr> LandmarksType;
    typedef std::unordered_map<unsigned long, Frame::Ptr> KeyframesType;
    
    Map() {}
    
    void InsertKeyFrame(Frame::Ptr frame);
    
    void InsertMapPoint(Mappoint::Ptr map_point);
    
    LandmarksType GetAllMapPoints(){
        std::unique_lock<std::mutex> lck(data_mutex_);
        return landmarks_;
    }
    
    KeyframesType GetAllKeyFrames(){
    std::unique_lock<std::mutex> lck(data_mutex_);
    return keyframes_;
    }
    
    LandmarksType GetActiveMapPoints(){
        std::unique_lock<std::mutex> lck(data_mutex_);
        return active_landmarks_;
    }
    
    KeyframesType GetActiveKeyFrame(){
        std::unique_lock<std::mutex> lck(data_mutex_);
        return active_keyframes_;
    }
    
    void CleanMap();
    
private:
    void RemoveOldkeyframe();
    
    std::mutex data_mutex_;
    LandmarksType landmarks_;
    LandmarksType active_landmarks_;
    KeyframesType keyframes_;
    KeyframesType active_keyframes_;
    
    Frame::Ptr current_frame_=nullptr;
    
    int num_active_keyframes_=7;
};
    

#endif // MAP_H
